package com.shgsh.server.utils;


import com.shgsh.server.model.IMMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.net.Socket;

/**
 * Created by pcc on 2015/8/13.
 */
public class ServerConClientThread extends Thread{
    Logger logger = LoggerFactory.getLogger(ServerConClientThread.class);
    Socket s;
    public ServerConClientThread(Socket s) {
        this.s = s;
    }

    @Override
    public void run() {
        while (true){
            ObjectInputStream ois = null;
            IMMessage message = null;
            try {
                if(s != null) {
                    logger.debug("this is the thread for " + s.getRemoteSocketAddress().toString());
                    ois = new ObjectInputStream(s.getInputStream());
                    message = (IMMessage) ois.readObject();
                    logger.debug(" id = " + message.getSendId() + "thread = " + s.getRemoteSocketAddress().toString());
                    //对从客户端取得的消息做相应的处理
                    DoWhatAndSendMes.sendMes(message);
                }
            } catch (Exception e) {
                try {
                    if(s != null) {
                        s.close();
                    }
                    if(ois != null) {
                        ois.close();
                    }
                    logger.debug("the thread close! for " + s.getRemoteSocketAddress().toString());
                } catch (IOException e1) {
                }
                e.printStackTrace();
            }
        }
    }
}

